package com.xhu.servlet;

import com.xhu.bean.pojo.ChanceManage;
import com.xhu.service.ChanceService;
import com.xhu.service.impl.ChanceServiceImpl;
import com.xhu.utils.BaseServlet;
import com.xhu.utils.FormatUtils;
import com.xhu.utils.ResultData;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@WebServlet("/CmServlet/*")
public class ChanceServlet extends BaseServlet {


    public ResultData add(HttpServletRequest request){


        String chance_code=request.getParameter("chance_code");
        String chance_name=request.getParameter("chance_name");
        String chance_typename=request.getParameter("chance_typename");
        String content=request.getParameter("content");
        String des=request.getParameter("des");


        ChanceManage chanceManage=new ChanceManage();
        chanceManage.setChance_code(chance_code);
        chanceManage.setChance_typename(chance_typename);
        chanceManage.setDes(des);
        chanceManage.setCm_content(content);
        chanceManage.setChance_name(chance_name);
        chanceManage.setAssigner_name("admin");
        chanceManage.setEntry_name("admin");
        chanceManage.setCm_month((new Date()).toString());
        chanceManage.setOperator_name("admin");
        chanceManage.setOrigin("网络");
        chanceManage.setCurrent_name("admin");
        chanceManage.setLast_optime((new Date()).toString());


        ChanceService chanceService=new ChanceServiceImpl();
        boolean tf=chanceService.add(chanceManage);
        return ResultData.ok().msg(tf?"添加成功":"添加失败");
    }

    public ResultData list(HttpServletRequest request){
        Integer page= FormatUtils.stringFormatInteger(request.getParameter("page"));
        Integer limit=FormatUtils.stringFormatInteger(request.getParameter("limit"));

        ChanceService chanceService=new ChanceServiceImpl();
        List<ChanceManage> list=chanceService.queryPage(page,limit);


        return ResultData.ok().data(list);
    }

    public ResultData list_search(HttpServletRequest request){
        Integer page= FormatUtils.stringFormatInteger(request.getParameter("page"));
        Integer limit=FormatUtils.stringFormatInteger(request.getParameter("limit"));
        String search_name=request.getParameter("search_name");
        if(search_name==""){
            ChanceService chanceService=new ChanceServiceImpl();
            List<ChanceManage> list=chanceService.queryPage(page,limit);
            return ResultData.ok().data(list);
        }else {
            ChanceService chanceService=new ChanceServiceImpl();
            List<ChanceManage> list=chanceService.queryPage(page,limit,search_name);
            return ResultData.ok().data(list);
        }


    }

    public ResultData distribute(HttpServletRequest request){

        String chance_id=request.getParameter("chance_id");

        String chance_name=request.getParameter("chance_name");
        String origin=request.getParameter("origin");
        String chance_typename=request.getParameter("chance_typename");
        String entry_name=request.getParameter("entry_name");
        String operator_name=request.getParameter("operator_name");
        String cm_content=request.getParameter("cm_content");
        String emp_name=request.getParameter("emp_name");

        ChanceManage chanceManage=new ChanceManage();
        //chanceManage.setCreate_time(new Date());
        chanceManage.setChance_name(chance_name);
        chanceManage.setOrigin(origin);
        chanceManage.setChance_typename(chance_typename);
        chanceManage.setEntry_name(entry_name);
        chanceManage.setOperator_name(operator_name);
        chanceManage.setCm_content(cm_content);
        chanceManage.setCurrent_name(emp_name);

        ChanceService chanceService=new ChanceServiceImpl();
        boolean tf=chanceService.upData(chanceManage,chance_id);
        return ResultData.ok().msg(tf?"分配成功":"分配失败");
    }

    public ResultData upData(HttpServletRequest request){

        String chance_id=request.getParameter("chance_id");

        String chance_code=request.getParameter("chance_code");
        String chance_name=request.getParameter("chance_name");
        String chance_typename=request.getParameter("chance_typename");
        String cm_conent=request.getParameter("cm_conent");
        String des=request.getParameter("des");


        ChanceManage chanceManage=new ChanceManage();
        chanceManage.setChance_code(chance_code);
        chanceManage.setChance_typename(chance_typename);
        chanceManage.setChance_name(chance_name);
        chanceManage.setCm_content(cm_conent);
        chanceManage.setDes(des);

        ChanceService chanceService=new ChanceServiceImpl();
        boolean tf=chanceService.distribute(chanceManage,chance_id);
        return ResultData.ok().msg(tf?"更新成功":"更新失败");

    }

    public ResultData del(HttpServletRequest request){
        String chance_id=request.getParameter("chance_id");

        System.out.println("del中的:"+chance_id);
        ChanceService chanceService=new ChanceServiceImpl();
        boolean tf=chanceService.del(chance_id);
        return ResultData.ok().msg(tf?"删除成功":"删除失败");

    }
}
